package com.cch.demo.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cch.demo.bus.domain.Customer;
import com.cch.demo.bus.domain.Provider;
import com.cch.demo.bus.service.CustomerService;
import com.cch.demo.bus.vo.CustomerVo;
import com.cch.demo.sys.domain.Permission;
import com.cch.demo.sys.util.Constast;
import com.cch.demo.sys.util.DataGridView;
import com.cch.demo.sys.util.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华哥
 * @since 2020-03-10
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private CustomerService customerServiceImpl;

    @RequestMapping("/loadAllCustomer")
    public DataGridView loadAllCustomer(CustomerVo customerVo){
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(customerVo.getCustomername()),"customername",customerVo.getCustomername());
        queryWrapper.like(StringUtils.isNotBlank(customerVo.getTelephone()),"telephone",customerVo.getTelephone());
        queryWrapper.like(StringUtils.isNotBlank(customerVo.getConnectionperson()),"connectionperson",customerVo.getConnectionperson());
        IPage<Customer> page = new Page<>(customerVo.getPage(),customerVo.getLimit());
        customerServiceImpl.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @RequestMapping("addCustomer")
    public ResultObj addCustomer(CustomerVo customerVo){
        try{
            customerServiceImpl.save(customerVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    @RequestMapping("updateCustomer")
    public ResultObj updateCustomer(CustomerVo customerVo){
        try{

            customerServiceImpl.updateById(customerVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    @RequestMapping("deleteCustomer")
    public ResultObj deleteCustomer(Integer id){
        try{
            customerServiceImpl.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("batchDeleteCustomer")
    public ResultObj BatchDeleteCustomer(CustomerVo customerVo){
        try{
            List<Serializable> idlist = new ArrayList<>();
            for (Integer id:customerVo.getIds()){
                idlist.add(id);
            }
            customerServiceImpl.removeByIds(idlist);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("loadAllCustomerForSelect")
    public DataGridView loadAllCustomerForSelect(){
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        //查询可用的供应商
        queryWrapper.eq("available", Constast.AVAILABLE_TRUE);
        List<Customer> list = customerServiceImpl.list();
        return new DataGridView(list);
    }
}

